package com.ayf.payment.game.api.controller;

import com.ayf.payment.game.api.dto.SiteStatisticsDTO;
import com.ayf.payment.game.api.service.SiteStatisticsService;
import com.ayf.payment.game.api.utils.DateUtils;
import com.ayf.payment.game.api.utils.IPUtils;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RequestMapping("/v/site/statistics")
@RestController
public class SiteStatisticsController {

    @Autowired
    private SiteStatisticsService siteStatisticsService;

    /**
     * 流量统计接口
     * @param request
     * @return
     */
    @GetMapping("/flow")
    public void flow(HttpServletRequest request) {
        String m = request.getParameter("m");
        if (StringUtils.isEmpty(m)) {
            return;
        }
        Integer merchantId = Integer.valueOf(m);
        String siteUrl = request.getParameter("s");
        if (StringUtils.isEmpty(siteUrl)) {
            siteUrl = getSiteUrl("直接登录");
            //return;
        }else {
            siteUrl = getSiteUrl(siteUrl);
        }
        try {
            String playerIp = IPUtils.getIp(request);
            LogPortal.info("来源统计 商户ID:{} 玩家IP:{} 发布站URL:{}",merchantId,playerIp,siteUrl);
            SiteStatisticsDTO siteStatisticsDTO = new SiteStatisticsDTO();
            siteStatisticsDTO.setMerchantId(merchantId);
            siteStatisticsDTO.setSiteUrl(siteUrl);
            Date currentDate = DateUtils.getNowDate();
            siteStatisticsDTO.setGenerateTimeIndex(DateUtils.dateToInteger(currentDate));
            siteStatisticsDTO.setCreateTime(currentDate);
            siteStatisticsDTO.setPlayerIp(playerIp);
            siteStatisticsService.saveSiteStatistics(siteStatisticsDTO);
        } catch (Exception e) {
            LogPortal.error("商户ID:{} 流量统计失败，发生了{}异常，异常描述:{}",e,merchantId,e.getClass().getName(),e.getMessage());
        }
    }

    /**
     * 获取网站域名信息
     * @param siteUrl
     * @return
     */
    private String getSiteUrl(String siteUrl) {
        String [] siteUrlArry=siteUrl.split("//");
        if (siteUrlArry.length>1){
            String [] siteUrlArry2=siteUrlArry[1].split("/");
            siteUrl = siteUrlArry2[0];
        }
        return siteUrl;
    }

    /**
     * 游戏人数统计接口
     * @param request
     * @return
     */
    @GetMapping("/players")
    public void players(HttpServletRequest request) {
        String m = request.getParameter("m");
        if (StringUtils.isEmpty(m)) {
            return;
        }
        Integer merchantId = Integer.valueOf(m);
        try {
            String playerIp = IPUtils.getIp(request);
            LogPortal.info("游戏人数统计 商户ID:{} 玩家IP:{}",merchantId,playerIp);
            SiteStatisticsDTO siteStatisticsDTO = new SiteStatisticsDTO();
            siteStatisticsDTO.setMerchantId(merchantId);
            siteStatisticsDTO.setPlayerIp(playerIp);
            Date currentDate = DateUtils.getNowDate();
            siteStatisticsDTO.setGenerateTimeIndex(DateUtils.dateToInteger(currentDate));
            siteStatisticsDTO.setCreateTime(currentDate);
            siteStatisticsService.saveSiteStatisticsPlayers(siteStatisticsDTO);
        } catch (Exception e) {
            LogPortal.error("商户ID:{} 游戏人数统计失败，发生了{}异常，异常描述:{}",e,merchantId,e.getClass().getName(),e.getMessage());
        }
    }
}
